<?php

// Namespace

namespace Loop;


/**
 * CRichDocumentationManager allows to create rich documentation	
 */

class CRichDocumentationManager
{
	/**
	 * Edit the rich documentation content 
	 *
	 * @param 	string 	$content 		The content to be put inside the rich documentation
	 * @param 	string 	$pathToFile 	The file path of the rich documentation
	 *
	 * @required $content
	 * @required $pathToFile
	 * 
	 * @return 	string 	The content formated as HTML
	 */

	public function api_editRichDocumentation()
	{
		// Get variables

		$content = fv('content', true);
		$pathToFile = fv('pathToFile', true);


		// Call

		$htmlContent = $this->editRichDocumentation($content, $pathToFile);


		return $htmlContent;
	}
	

	/**
	 * Gets and returns the rich documentation content following the given arguments
	 *
	 * @param 	string 	$concept 		The concept 
	 * @param 	string 	$extensionId 	The Id of the extension
	 * @param 	string 	$file 			The file name 
	 * @param 	string 	$method 		The name of the medthod
	 *
	 * @return 	array 	The array which contains the asked rich documentation content
	 */

	public function getRichDocumentation($concept = null, $extensionId = null, $file = null, $method = null)
	{
		// If there is no parameters

		if 
		(
			is_null($concept) === true &&
			is_null($extensionId) === true &&
			is_null($file) === true &&
			is_null($method) === true 
		)
		{
			e(EXCEPTION_VARIABLE_NOT_FOUND);
		}


		//

		$result = array();


		// Get the path to extention

		$pathToExtension = app()->getPathHelper()->convertExtensionIdToPath($extensionId);


		// If there is no concept, no method and if the name of the file equal the name of extension

		if 
		(
			is_null($concept) === true &&
			is_null($method) === true &&
			$extensionId == $file
		)
		{
			// Construct the path to documentation 

			$pathToDocumentation = path 
			(
				array
				(			
					$pathToExtension,
					'Documentation',
				),
				false,
				false
			);

			$fileName = $file . '.txt';


			// Construct the path to the file 

			$pathToFile= $pathToDocumentation . $fileName;		


			return $this->getContent($pathToFile);
		}


		// If there is  no method and the name of the file equal the name of concept

		if 
		(
			is_null($method) === true &&
			$concept == $file
		)
		{
			// Construct the path to documentation 

			$pathToDocumentation = path 
			(
				array
				(			
					$pathToExtension,
					'Documentation',
					$concept,
				),
				false,
				false
			);

			$fileName = $concept . '.txt';


			// Construct the path to the file 

			$pathToFile= $pathToDocumentation . $fileName;		


			return $this->getContent($pathToFile);
		}



		// If there is  no method 

		if (is_null($method) === true )
		{
			$fileName = $file . '.txt';

			// Construct the path to documentation 

			$pathToDocumentation = path 
			(
				array
				(			
					$pathToExtension,
					'Documentation',
					$concept,
					$file
				),
				false,
				false
			);


			// Construct the path to the file 

			$pathToFile= $pathToDocumentation . $fileName;		


			return $this->getContent($pathToFile);
		}	


		// if there is an extensionId, a directory, a concept, a method

		if 
		(
			is_null($concept) === false &&
			is_null($method) === false &&	
			is_null($file) === false
		)
		{
			$fileName = $method . '.txt';

			// Construct the path to documentation 

			$pathToDocumentation = path 
			(
				array
				(			
					$pathToExtension,
					'Documentation',
					$concept,
					$file
				),
				false,
				false
			);

			// Construct the path to the file 

			$pathToFile= $pathToDocumentation . $fileName;		


			return $this->getContent($pathToFile);	
		}
	}


	/**
	 * @deprecated
	 */

	public function editRichDocumentation($content, $pathToFile)
	{
		// Put content in file

		file_put_contents($pathToFile, $content);


		return app()->getStringHelper()->formatMarkdownToHtml($content);
	}


	/**
	 * Gets and returns the file content of a given file path
	 *
	 * @param 	string 	$pathToFile 	The file path
	 *
	 * @return 	array 	The array which contains the asked file content
	 */

	public function getContent($pathToFile)
	{
		// Store the path to the file

		$result['pathToFile'] = $pathToFile;


		if (file_exists($pathToFile))
		{
			$fileContent = file_get_contents($pathToFile);


			// Format text to html

			$result['content'] = app()->getStringHelper()->formatMarkdownToHtml($fileContent);
			$result['markdownContent'] = $fileContent;
		}
		else
		{
			// Create the file
			
			$result['content'] =  'No content';
			$result['markdownContent'] = '';
		}
	

		return $result;
	}
}

?>
